<?php

namespace backend\modules\user\controllers;

use common\models\user\Member;
use Yii;
use common\models\base\SearchModel;
use common\traits\Curd;
use common\models\user\Buyer;
use common\enums\StatusEnum;
use backend\controllers\BaseController;

/**
 * 客户端
 *
 * Class ClientController
 * @package backend\modules\member\controllers
 * @author jianyan74 <751393839@qq.com>
 */
class BuyerController extends BaseController
{
    use Curd;

    /**
     * @var Client
     */
    public $modelClass = Buyer::class;

    /**
     * 首页
     *
     * @return string
     * @throws \yii\web\NotFoundHttpException
     */
    public function actionIndex()
    {
        $searchModel = new SearchModel([
            'model' => $this->modelClass,
            'scenario' => 'default',
            'partialMatchAttributes' => ['member_name'], // 模糊查询
            'defaultOrder' => [
                'id' => SORT_DESC
            ],
            'pageSize' => $this->pageSize
        ]);

        $dataProvider = $searchModel
            ->search(Yii::$app->request->queryParams);
        $dataProvider->query
            ->where(['>','member_auth_state',0])
            ->andWhere(['>=', 'status', StatusEnum::DISABLED]);

        return $this->render($this->action->id, [
            'dataProvider' => $dataProvider,
            'searchModel' => $searchModel,
        ]);
    }

    /**
     * 查看
     *
     * @return mixed
     */
    public function actionEdit()
    {
        $id = Yii::$app->request->get('id', null);
        $model = $this->findModel($id);
        return $this->render($this->action->id, [
            'model' => $model,
        ]);
    }

    public function actionAudit(){
        $id = Yii::$app->request->get();
        if ($id['OK']){
                $data =Member::findOne($id['id']);
                $data->member_auth_state = $id['OK'];
                if($data->save()){
                    return $this->message("审核成功", $this->redirect(Yii::$app->request->referrer));
                }
        }
        if ($id['NO']){
            $data =Member::findOne($id['id']);
            $data->member_auth_state = $id['NO'];
            if($data->save()){
                return $this->message("审核成功", $this->redirect(Yii::$app->request->referrer));
            }
        }
    }

}